import { Injectable, NotAcceptableException } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { PrismaService } from 'src/prisma/prisma.service'

@Injectable()
export class FoodService {
  constructor(private readonly prismaService: PrismaService) {}

  async createFood(data: Prisma.FoodCreateInput) {
    try {
      const food = await this.prismaService.food.create({
        data
      })
      return food
    } catch (error) {
      throw new NotAcceptableException('为找到该种类')
    }
  }

  async getFoods() {
    const foods = await this.prismaService.food.findMany()
    return foods
  }

  async searchFoods(word: string) {
    const foods = await this.prismaService.food.findMany({
      where: {
        name: {
          contains: word
        }
      }
    })
    return foods
  }
}
